package com.example.servlet;

import com.example.entity.IndexInfo;
import com.example.entity.customerAdmin.*;
import com.example.service.CustomService;
import com.example.utils.BaseServlet;
import com.example.utils.ResultData;
import com.example.vo.IndexInfoDto;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

@WebServlet("/custom")
public class CustomServlet extends BaseServlet {
    CustomService customService=new CustomService();
    @Override
    public Class getChildClass() {
        return this.getClass();
    }
    public ResultData allCustom(HttpServletRequest request, HttpServletResponse response){
        String uname = request.getParameter("uname");
        if(uname!=null){
            String status = request.getParameter("states");
            int type= Integer.parseInt(request.getParameter("type"));
            String starRating = request.getParameter("starRating");
            CustomerAdmin custom=new CustomerAdmin();
            custom.setName(uname);
            custom.setStates(status);
            custom.setType(type);
            custom.setStarRating(starRating);
            return customService.selectPart(custom);
        }
        int page = Integer.parseInt(request.getParameter("page"));
        int limit = Integer.parseInt(request.getParameter("limit"));
        return customService.selectAll(page,limit);
    }
    public void getBtnAll(HttpServletRequest request,HttpServletResponse response) throws Exception {
        int pid = Integer.parseInt(request.getParameter("id"));
        List<IndexInfo> indexInfoList =customService.getBtn(pid);
        List<IndexInfoDto> indexInfoDtos=new ArrayList<>();
        for (IndexInfo indexInfo:indexInfoList) {
            IndexInfoDto indexInfoDto=new IndexInfoDto();
            indexInfoDto.setResUrl(indexInfo.getHref());
            indexInfoDtos.add(indexInfoDto);
        }
        HttpSession session1 = request.getSession();
        session1.setAttribute("cusBtn",indexInfoDtos);
        Object cusBtn = session1.getAttribute("cusBtn");
        request.getRequestDispatcher("/admin/page/system/user/userList.jsp").forward(request,response);
    }
    public void beforeDetail(HttpServletRequest request, HttpServletResponse response) throws IOException {
        String name = request.getParameter("name");
        ResultData resultData=customService.selectByName(name);
        for (Object datum : resultData.getData()) {
            CustomerAdmin custom=(CustomerAdmin) datum;
            HttpSession session = request.getSession();
            session.setAttribute("custom",custom);
            response.sendRedirect("/admin/page/system/user/detail.jsp");
            return;
        }
    }
    public ResultData selectContact(HttpServletRequest request,HttpServletResponse response){
        int id = Integer.parseInt(request.getParameter("id"));
        return customService.selectContact(id);
    }
   public ResultData selectCusName(HttpServletRequest request,HttpServletResponse response){
       String name = request.getParameter("name");
       return customService.selectCusName(name);
   }
   public ResultData selectExtreById(HttpServletRequest request,HttpServletResponse response){
       int id = Integer.parseInt(request.getParameter("id"));
       return customService.selectExtreById(id);
   }
   public ResultData selectFollowDetail(HttpServletRequest request,HttpServletResponse response){
       String name = request.getParameter("name");
       return customService.selectFollowDetail(name);
   }
   public ResultData selectBelCus(HttpServletRequest request,HttpServletResponse response){
       String name = request.getParameter("name");
       return customService.selectBelCus(name);
   }
   public ResultData isUname(HttpServletRequest request,HttpServletResponse response){
       String name = request.getParameter("uname");
       return customService.isUname(name);
   }
   public ResultData addCusConExCusAd(HttpServletRequest request,HttpServletResponse response){
        //先添加联系人表，获取联系人id再添加添加客户表，最后额外表和客户完全信息表
       String contact = request.getParameter("concat");
       int gender = Integer.parseInt(request.getParameter("personal"));
       String role = request.getParameter("role");
       String deptJob = request.getParameter("dept_job");
       String phone = request.getParameter("phone");
       String fixedPhone = request.getParameter("fixedPhone");
       String transmit = request.getParameter("transmit");
       String email = request.getParameter("email");
       String birthday = request.getParameter("birthday");
       String qq = request.getParameter("qq");
       String area = request.getParameter("area");
       String address = request.getParameter("address");
       String tip = request.getParameter("tip");
       ContactUser contactUser=new ContactUser();
       contactUser.setName(contact);
       contactUser.setPhone(phone);
       contactUser.setRole(role);
       contactUser.setDeptJob(deptJob);
       contactUser.setBirthday(birthday);
       contactUser.setEmail(email);
       contactUser.setFixedPhone(fixedPhone);
       contactUser.setTransPhone(transmit);
       contactUser.setQq(qq);
       contactUser.setArea(area);
       contactUser.setAddress(address);
       contactUser.setTip(tip);
       contactUser.setSex(gender);
       //添加名称
       String uname = request.getParameter("uname");
       String helpName = request.getParameter("helpName");
       String belIndustry = request.getParameter("bel_industry");
       String cusType = request.getParameter("cusType");
       String custatus = request.getParameter("custatus");
       String custar = request.getParameter("custar");
       String source = request.getParameter("source");
       String parent = request.getParameter("parent");
       Custom custom=new Custom();
       custom.setName(uname);
       custom.setCusType(cusType);
       custom.setCusStar(custar);
       custom.setCusState(custatus);
       custom.setHelpName(helpName);
       custom.setBelIndustry(belIndustry);
       custom.setResource(source);
       ResultData resultData = customService.selectByName(parent);
       List<Custom> data = resultData.getData();
       if(!Objects.isNull(data)){
           for (Custom custom1:data) {
               custom.setPid(custom1.getId());
           }
       }
       //新建额外信息
       String cusarea = request.getParameter("cusarea");
       String cusaddress = request.getParameter("cusaddress");
       String scale = request.getParameter("scale");
       Extra extra=new Extra();
       extra.setArea(cusarea);
       extra.setAddress(cusaddress);
       extra.setScale(scale);
       //获取跟进记录
       String followTime = request.getParameter("followTime");
       String folContent = request.getParameter("fol_content");
       Follow follow=new Follow();
       follow.setTime(followTime);
       follow.setRemarks(folContent);
       follow.setCusName(uname);
       follow.setContactUser(contact);
       return customService.addCusConExCusAd(contactUser,custom,extra,follow);
   }
   public ResultData selectAll(HttpServletRequest request,HttpServletResponse response){
       return customService.selectAll();
   }
}
